<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of review
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Review extends MY_Controller {

    function review() {
        parent :: __construct();
        $this->load->model('pickup_model');
        $this->load->model('location_model');
        $this->load->model('management_model');
        $this->load->model('listing_model');
        $this->load->model('requirement_model');

        $this->template->set_layout('body_top');
    }

    function index() {

        $data['make']['info'] = $this->management_model->get_makes('CAR');
        $data['pickup']['ratting'] = $this->pickup_model->get_pickup(14);


        $this->template->title('Welcome', 'carz.com')
                ->set_partial('header', 'header/header')
                ->set_partial('recent', 'recent/post')
                ->set_partial('customer_care', 'sidebar/customer_care')
                ->set_partial('social_share', 'sidebar/social_share')
                ->set_partial('search_reviews', 'sidebar/search_reviews')
                ->set_partial('recent_news', 'sidebar/recent_news')
                ->set_partial('recent_reviews', 'sidebar/recent_reviews')
                ->set_partial('footer', 'footer/footer')
                ->build('pages/review', $data);
    }

//--Save Review Information (Subha)----//  
    function review_submit($type = 'CAR') {
        if ($type == 'CAR') {
            $this->form_validation->set_message('required', '* Required');
            $this->form_validation->set_message('valid_email', '* Invalid Email');

            $this->form_validation->set_rules('car_make', 'Required', 'trim|required');
            $this->form_validation->set_rules('car_model', 'Required', 'trim|required');
            $this->form_validation->set_rules('car_version', 'Required', 'trim|required');

            $this->form_validation->set_rules('exterior', 'exterior', 'trim|required');
            $this->form_validation->set_rules('comfort', 'Required', 'trim|required');
            $this->form_validation->set_rules('performance', 'Required', 'trim|required');
            $this->form_validation->set_rules('fuel', 'Required', 'trim|required');
            $this->form_validation->set_rules('value', 'Required', 'trim|required');

            $this->form_validation->set_rules('name', 'Name', 'trim|required');
            $this->form_validation->set_rules('email', 'Required', 'trim|required|valid_email');

            $this->form_validation->set_rules('pros', 'Required', 'trim|required');
            $this->form_validation->set_rules('cons', 'Required', 'trim|required');

            $this->form_validation->set_rules('review_detail', 'Required', 'trim|required|min_length[150]|max_length[800]');

            if ($this->form_validation->run() == FALSE) {
                $this->index();
            } else {
                //--Save Post Data --//
                $post_data = array(
                    'type' => 'CAR',
                    'make' => $this->input->post('car_make'),
                    'model' => $this->input->post('car_model'),
                    'version' => $this->input->post('car_version'),
                    'exterior' => $this->input->post('exterior'),
                    'comfort' => $this->input->post('comfort'),
                    'performance' => $this->input->post('performance'),
                    'fuel' => $this->input->post('fuel'),
                    'value' => $this->input->post('value'),
                    'name' => $this->input->post('name'),
                    'email' => $this->input->post('email'),
                    'pros' => $this->input->post('pros'),
                    'cons' => $this->input->post('cons'),
                    'description' => $this->input->post('review_detail'),
                    'posted_on' => date('d-M-Y', now()),
                    'ip' => get_ip(),
                    'status' => 1
                );
                if ($post_data) {
                    $this->db->insert('review_master', $post_data);
                    $this->session->set_flashdata('message', 'Review Posted ! Waiting for Moderation ...');
                    redirect('review');
                } else {
                    $this->session->set_flashdata('message', 'Invalid Data entry !');
                    $this->index();
                }
            }
        }
    }

//--Review Detail --------//Subha
    function detail($desc = '', $id = 0) {

        $sql = "Select * from review_master where id=$id and status=51";
        $query = $this->db->query($sql);
        $row = $query->row();

        $data['review']['info'] = $row;
        $this->template->title('Welcome', 'carz.com')
                ->set_partial('header', 'header/header')
                ->set_partial('recent', 'recent/post')
                ->set_partial('customer_care', 'sidebar/customer_care')
                ->set_partial('social_share', 'sidebar/social_share')
                ->set_partial('search_reviews', 'sidebar/search_reviews')
                ->set_partial('recent_news', 'sidebar/recent_news')
                ->set_partial('recent_reviews', 'sidebar/recent_reviews')
                ->set_partial('footer', 'footer/footer')
                ->build('pages/review_detail', $data);
    }

    function search() {



        $this->form_validation->set_message('required', '* Required');
        $this->form_validation->set_rules('car_make', 'Required', 'trim|required');
        $this->form_validation->set_rules('car_model', 'Required', 'trim|required');
        $this->form_validation->set_rules('car_version', 'Required', 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $this->session->set_flashdata('error', 'Invalid input ..! ');
            redirect($this->input->post('redirect'));
        } else {
            //--Bind Data --//
            $car_make = intval($this->input->post('car_make'));
            $car_model = intval($this->input->post('car_model'));
            $car_version = intval($this->input->post('car_version'));

            $sql = "Select * from review_master where status=51 and make=$car_make and model=$car_model and version=$car_version order by id desc limit 25 ";
            $query = $this->db->query($sql);
            $result = $query->result();

            $data['review']= $result;
            $data['car_version'] = $car_version;
            
            //print_r($data['review']);
            //die();
            
            
            $this->template->title('Welcome', 'carz.com')
                    ->set_partial('header', 'header/header')
                    ->set_partial('recent', 'recent/post')
                    ->set_partial('customer_care', 'sidebar/customer_care')
                    ->set_partial('social_share', 'sidebar/social_share')
                    ->set_partial('search_reviews', 'sidebar/search_reviews')
                    ->set_partial('recent_news', 'sidebar/recent_news')
                    ->set_partial('recent_reviews', 'sidebar/recent_reviews')
                    ->set_partial('footer', 'footer/footer')
                    ->build('pages/review_search_result', $data);
        }
    }

}

?>
